package com.exploadingelephants.engine;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import ch.exploadingelephants.graph.DotColor;
import ch.exploadingelephants.model.ConveyorBeltItem;
import ch.exploadingelephants.model.ConveyorDirection;
import ch.exploadingelephants.model.EndItem;
import ch.exploadingelephants.model.Level;
import ch.exploadingelephants.model.Machine;
import ch.exploadingelephants.model.RBBifurcationItem;
import ch.exploadingelephants.model.StartItem;
import ch.exploadingelephants.model.WriterItem;

import com.exploadingelephants.util.LevelIO;
import com.exploadingelephants.views.MachineView;

/**
 * @author Sandro De Zanet
 * 
 */
public class LevelEditorActivity extends Activity {
    private MachineView machineView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	int id = 100;

	final Level level = new Level("test", false);
	level.setMachine(createSimpleMachine());

	RelativeLayout layout = new RelativeLayout(this);
	layout.setGravity(Gravity.CENTER_HORIZONTAL);

	LayoutParams params = createParams();
	params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

	TextView titleView = new TextView(this);
	titleView.setText(level.getTitle());
	titleView.setId(id++);
	layout.addView(titleView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, titleView.getId());
	Machine machine = level.getMachine();
	machineView = new MachineView(this, machine);
	machineView.setId(id++);
	layout.addView(machineView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, machineView.getId());
	TextView descriptionView = new TextView(this);
	descriptionView.setText(level.getDescription());
	descriptionView.setId(id++);
	layout.addView(descriptionView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, descriptionView.getId());
	final TextView resultView = new TextView(this);
	resultView.setText("no result");
	resultView.setId(id++);
	layout.addView(resultView, params);

	params = createParams();
	params.addRule(RelativeLayout.BELOW, resultView.getId());
	Button deleteButton = new Button(this);
	deleteButton.setId(id++);
	deleteButton.setText("X");
	deleteButton.setOnClickListener(new OnClickListener() {

	    public void onClick(View v) {
		machineView.setActiveItem(null);
	    }
	});
	layout.addView(deleteButton, params);

	params = createParams();
	params.addRule(RelativeLayout.RIGHT_OF, deleteButton.getId());
	params.addRule(RelativeLayout.BELOW, resultView.getId());
	Button saveButton = new Button(this);
	saveButton.setId(id++);
	saveButton.setText("Save");
	saveButton.setOnClickListener(new OnClickListener() {

	    public void onClick(View v) {
		Level level = new Level();
		level.setMachine(createSimpleMachine());
		level.setSolution(machineView.getMachineGraph());
		try {
		    LevelIO.storeLevel(LevelEditorActivity.this, level);
		} catch (FileNotFoundException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
	    }
	});
	layout.addView(saveButton, params);

	View lastCb = null;
	View lastRb = null;
	View lastRw = null;
	View lastBw = null;
	for (final ConveyorDirection direction : ConveyorDirection.values()) {
	    // Conveyor belt
	    ImageButton cb = new ImageButton(this);
	    cb.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		    direction.getResourceIDs().get(0)));
	    cb.setId(id++);
	    RelativeLayout.LayoutParams lp = createParams();
	    if (lastCb != null) {
		lp.addRule(RelativeLayout.RIGHT_OF, lastCb.getId());
		lp.addRule(RelativeLayout.BELOW, deleteButton.getId());
	    } else {
		lp.addRule(RelativeLayout.BELOW, deleteButton.getId());
	    }
	    lastCb = cb;
	    cb.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    machineView.setActiveItem(new ConveyorBeltItem(direction));
		}
	    });
	    layout.addView(cb, lp);

	    // RedBlue Bifurcation
	    lp = createParams();
	    if (lastRb != null) {
		lp.addRule(RelativeLayout.BELOW, cb.getId());
		lp.addRule(RelativeLayout.RIGHT_OF, lastRb.getId());
	    } else {
		lp.addRule(RelativeLayout.BELOW, cb.getId());
	    }

	    ImageButton rb = new ImageButton(this);
	    lastRb = rb;
	    rb.setId(id++);
	    rb.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		    direction.getRbResourceID()));
	    layout.addView(rb, lp);
	    rb.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    machineView.setActiveItem(new RBBifurcationItem(direction));
		}
	    });

	    // Red write
	    lp = createParams();
	    if (lastRw != null) {
		lp.addRule(RelativeLayout.BELOW, rb.getId());
		lp.addRule(RelativeLayout.RIGHT_OF, lastRw.getId());
	    } else {
		lp.addRule(RelativeLayout.BELOW, rb.getId());
	    }

	    ImageButton rw = new ImageButton(this);
	    lastRw = rw;
	    rw.setId(id++);
	    rw.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		    direction.getRwResourceID()));
	    layout.addView(rw, lp);
	    rw.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    machineView.setActiveItem(new WriterItem(DotColor.RED,
			    direction));
		}
	    });

	    // Blue write
	    lp = createParams();
	    if (lastBw != null) {
		lp.addRule(RelativeLayout.BELOW, rw.getId());
		lp.addRule(RelativeLayout.RIGHT_OF, lastBw.getId());
	    } else {
		lp.addRule(RelativeLayout.BELOW, rw.getId());
	    }

	    ImageButton bw = new ImageButton(this);
	    lastBw = bw;
	    bw.setId(id++);
	    bw.setImageBitmap(BitmapFactory.decodeResource(getResources(),
		    direction.getBwResourceID()));
	    layout.addView(bw, lp);
	    bw.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
		    machineView.setActiveItem(new WriterItem(DotColor.BLUE,
			    direction));
		}
	    });
	}

	setContentView(layout);
    }

    private Machine createSimpleMachine() {
	Machine machine = new Machine();
	machine.set(2, 0, new StartItem());
	machine.set(2, 4, new EndItem());
	return machine;
    }

    private LayoutParams createParams() {
	return new RelativeLayout.LayoutParams(
		RelativeLayout.LayoutParams.WRAP_CONTENT,
		RelativeLayout.LayoutParams.WRAP_CONTENT);
    }

}
